package com.canyou.redmine.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.canyou.redmine.R;
import com.canyou.redmine.adapter.TaskAdapter;
import com.canyou.redmine.model.Issue;
import com.canyou.redmine.model.IssuePage;
import com.canyou.redmine.network.CanYouAPI;
import com.canyou.redmine.ui.IssueActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 任务列表
 *
 * @author Administrator
 */
public class TasksFragment extends Fragment {

    private RecyclerView rvTasks;
    private BaseQuickAdapter adapter;
    private List<Issue> issueList = new ArrayList<>();
    private RefreshLayout refreshLayout;

    private int pageIndex = 0;
    private int pageSize = 25;

    public TasksFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tasks, container, false);
        initUI(view);
        return view;
    }

    private void initUI(View view) {
        refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 0;
                getMyIssues(pageIndex, pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                getMyIssues(pageIndex, pageSize);

            }
        });

        rvTasks = view.findViewById(R.id.rv_tasks);
        rvTasks.setLayoutManager(new LinearLayoutManager(getContext()));

        adapter = new TaskAdapter(getActivity(), R.layout.list_item_task, issueList);

        rvTasks.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), IssueActivity.class);
                Issue issue = issueList.get(position);
                intent.putExtra("issue", JSON.toJSONString(issue));
                startActivity(intent);
            }
        });

        rvTasks.setAdapter(adapter);

        refreshLayout.autoRefresh();
    }

    /**
     * 获取我的任务
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     */
    private void getMyIssues(final int pageIndex, int pageSize) {
        CanYouAPI.getMyIssues(pageIndex, pageSize, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                IssuePage page = JSON.parseObject(responseString, IssuePage.class);
                if (page == null || page.getIssues() == null) {
                    return;
                }
                if (pageIndex == 0) {
                    issueList.clear();
                }
                issueList.addAll(page.getIssues());
                adapter.setNewData(issueList);
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore(true);
            }
        });
    }

}
